<?php
class Company extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('company_model');
	}


	public function delete($id) {
		$this->company_model->is_valid($id);
		$this->load->view('pages/delete_user_return');
	}
	
	public function index() {
		$data['title'] = "Company Details";
		$data['user_details'] = $this->company_model->get_details();
		$this->load->view('templates/header' , $data);
		$this->load->view('pages/user_details' , $data);
	}

	public function add() {
		$this->load->helper(array('form' , 'url'));

		$this->load->library('form_validation');

		$config = array(
				array(
						'field' => 'fname' ,
						'label' => 'First Name' ,
						'rules' => 'required|xss_clean'
				),
				array(
						'field' => 'mname' ,
						'label' => 'Middle Name' ,
						'rules' => 'required|xss_clean'
				),
				array(
						'field' => 'lname' ,
						'label' => 'Last Name' ,
						'rules' => 'required|xss_clean'
				),
				array(
						'field' => 'password' ,
						'label' => 'Password' ,
						'rules' => 'required|min_length[5]|matches[passconf]|xss_clean'
				),
				array(
						'field' => 'passconf' ,
						'label' =>	'Confirm Password' ,
						'rules' => 'required|xss_clean' ,
				),
				array(
						'field' => 'address' ,
						'label' => 'Address' ,
						'rules' => 'required|xss_clean'
				)
		);


		$this->form_validation->set_rules($config);

		if($this->form_validation->run() == FALSE) {
			$this->load->view('pages/register');
		} else {

			$data = array(
					'username' => $this->input->post('fname').'.'.$this->input->post('lname') ,
					'password' => $this->input->post('password') ,
					'fname'	=> $this->input->post('fname') ,
					'mname' => $this->input->post('mname') ,
					'lname' => $this->input->post('lname') ,
					'address' => $this->input->post('address')
					//id of admin or supervisor/manager
			);

			$this->company_model->insert($data);
			$this->load->view('pages/regsuccess');
		}
	}
	
	public function edit($id) { //load page using user id
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		
		$data['details'] = $this->company_model->find_by_id($id);

		$config = array(
			array(
				'field' => 'username' ,
				'label' => 'Username' ,
				'rules' => 'xss_clean'
			) , 
			array(
				'field' => 'fname' ,
				'label' => 'First Name' ,
				'rules' => 'required|xss_clean'
			),
			array(
				'field' => 'mname' ,
				'label' => 'Middle Name' ,
				'rules' => 'required|xss_clean'
			) ,
			array(
				'field' => 'lname' ,
				'label' => 'Last Name' ,
				'rules' => 'required|xss_clean'
			),
			array(
				'field' => 'address' ,
				'label' => 'Address' ,
				'rules' => 'required|xss_clean'
			)
		);
		
		$this->form_validation->set_rules($config);
		
		if($this->form_validation->run() == FALSE) {
			$this->load->view('pages/edit_user' , $data);
		} else {
			$data = array(
            	'username' => $this->input->post('fname').'.'.$this->input->post('lname') ,
            	'fname' => $this->input->post('fname') ,
				'mname' => $this->input->post('mname') ,
            	'lname' => $this->input->post('lname') ,
				'address' => $this->input->post('address')
        	);
        	
        	$this->company_model->edit($id , $data);
		}

		
	}
	
}
?>